
package com.jk.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class Codeutils {

	    private static final String
	            SERVER_URL="https://api.netease.im/sms/sendcode.action";
	    //网易云信分配的账号，请替换你在管理后台应用下申请的Appkey
	    private static final String
				APP_KEY="5404288be9520eaa4cfeb7131f023625";
	    //网易云信分配的密钥，请替换你在管理后台应用下申请的appSecret
		private static final String APP_SECRET="36138f543773";
	    //随机数
	    private static final String NONCE="123456789";
	    //短信模板ID
		private static final String TEMPLATEID="19478120";
	    //验证码长度，范围4～10，默认为4
	    private static final String CODELEN="6";

		public static void sendMessage(String phone, Map<String, Object> paramMap) {
			CloseableHttpClient httpClient = HttpClientBuilder.create().build();
			// 获取到当前时间 规定生成的加密规则时间 默认为5s
			String curTime = String.valueOf((new Date()).getTime() / 1000L);
			// 根据APP_SECRET+随机数+当前时间 生成加密认证规则 
			String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);
		    HttpPost httpPost = new HttpPost(SERVER_URL);
		
	        httpPost.addHeader("AppKey", APP_KEY);
	        //密钥随机数规则 生成加密的随机数字要与此数字一致 否则认证不通过
	        httpPost.addHeader("Nonce", NONCE);
	        //加密时间 要与生成密钥的时间一致
	        httpPost.addHeader("CurTime", curTime);
	        httpPost.addHeader("CheckSum", checkSum);
	        //请求报文格式 x-www-form-urlencoded 以from表单的格式提交数据
	        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
	        //创建一个页面表单集合 NameValuePair 表单元素 name代表表单中name值 value代表表单中的value值
	        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
	        //BasicNameValuePair 基本表单元素对象 key为input中的name value值为input中的value 
	        nvps.add(new BasicNameValuePair("templateid", TEMPLATEID));
	        nvps.add(new BasicNameValuePair("mobile", phone));
	        nvps.add(new BasicNameValuePair("codeLen", CODELEN));
	        try {
				httpPost.setEntity(new UrlEncodedFormEntity(nvps));
				CloseableHttpResponse response = httpClient.execute(httpPost);
				String responseInfo = EntityUtils.toString(response.getEntity());
	            Map responseMap = JSON.parseObject(responseInfo,Map.class);
	            //设置返回信息
	            paramMap.putAll(responseMap);
	        } catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally{
				if(httpClient != null){
					try {
						httpClient.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			} 
		}

}

